package com.himaci.gtbattle.model;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Kelas singleton untuk menyimpan log permainan
 *
 * @author XNPSDN
 */
public class Logger {

    private static Logger instance;
    public ArrayList<Action> gameActionLog;
    public int win;
    public int aiLevel;
    public int predictedMove;
    public int unpredictedMove;

    /*
     * Private konstruktor
     */
    private Logger() {
	gameActionLog = new ArrayList<>();
	win = 0;
	aiLevel = 0;
	predictedMove = 0;
	unpredictedMove = 0;
    }

    /**
     * Mendapatkan singleton instance
     * @return Instance dari kelas Logger
     */
    public static synchronized Logger getInstance() {
	if (instance == null) {
	    instance = new Logger();
	}
	return instance;
    }

    /**
     * Menambah aksi ke log
     * @param action Aksi yang mau ditambahkan
     */
    public void addLog(Action action) {
	gameActionLog.add(action);
	// print
	System.out.print("Executed : ");
	if (action != null) {
	    System.out.println(action.display());
	} else {
	    System.out.println("Skip Skill");
	}
    }

    /**
     * Mengambil log aksi berdasarkan nomor player
     * @param playerNumber Player number
     * @return List action log
     */
    public Collection<Action> getActionLog(int playerNumber) {
	ArrayList<Action> retVal = new ArrayList<>();
	for (Action action : gameActionLog) {
	    if (action.playerNumber == playerNumber) {
		retVal.add(action);
	    }
	}
	return retVal;
    }

    /**
     * Membersihkan log aksi
     */
    public void clear() {
	win = 0;
	aiLevel = 0;
	predictedMove = 0;
	unpredictedMove = 0;
	gameActionLog.clear();
    }

    /**
     * Di set pemenangnya buat dikirim
     * @param win Menang atau kalah atau seri (-1 kalah, 0 seri, 1 menang)
     * @param aiLevel 
     */
    public void setWinner(int win, int aiLevel) {
	this.win = win;
	this.aiLevel = aiLevel;
    }
}
